#include <GL/freeglut.h>

typedef GLfloat point2d[2];

void triangle(point2d a, point2d b, point2d c)
{
    glBegin(GL_TRIANGLES);
    glVertex2fv(a);
    glVertex2fv(b);
    glVertex2fv(c);
    glEnd();
}

void display()
{
    point2d v[3] = {{-1, -0.58}, {1, -0.58}, {0, 1.15}};
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0, 0, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    // 视口宽高比是否与观察窗口宽高比保持一致会影响图形畸变与否
    glViewport(0, 0, 300, 300);
    triangle(v[0], v[1], v[2]);
    glColor3f(1, 0, 0);
    glViewport(300, 0, 300, 300);
    glRotatef(90, 0, 0, 1);
    triangle(v[0], v[1], v[2]);
    glFlush();
}

void init()
{
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(-2, 2, -2, 2);

    glClearColor(1, 1, 1, 1);
}

void main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(600, 400);
    glutCreateWindow("Triangle");
    glutDisplayFunc(display);
    init();
    glutMainLoop();
}
